require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'


class TC_Announcements_test < Test::Unit::TestCase
  
    def startup
        $Subject = 'Announcement1'
        $Description = 'This is a test written to verify the functionality for creating an  announcement'
        $SubjectEdit = 'Editing the Announcement1'
        $DescriptionEdit = 'This is a test written to verify the functionality for editing an  announcement'
        @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
    
    
    def test_06_verifyLinks
        assert_equal $SITE_URL , ie.link(:text, 'Home').href
        assert_equal $SITE_URL+'blogs/', ie.link(:text, 'Blogs').href
        assert_equal $SITE_URL+'media/', ie.link(:text, 'Media').href
        assert_equal $SITE_URL+'forums/', ie.link(:text, 'Forums').href
        assert_equal $SITE_URL+'groups/', ie.link(:text, 'Groups').href
        assert_equal $SITE_URL+'controlpanel/default.aspx', ie.link(:text, 'Control Panel').href
      end
    def test_07_viewProfile
        assert(ie.link(:text,'View public profile').exists?)
        ie.link(:text,'View public profile').click
      end
    def test_08_addAnnouncement
        assert(ie.link(:text,'Add an Announcement').exists?)
        ie.link(:text,'Add an Announcement').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/MessageSubject$/).exists?)
        ie.frame(:index,1).text_field(:id,/MessageSubject$/).value = $Subject
        sleep 4
        ie.ie.Document.ParentWindow.execscript("Telligent_Modal._modalIframe.contentWindow.tinyMCE.get('ctl00_bcr_ctl01_ctl02_Message_ctl00_Editor').execCommand('mceSetContent', false,'"+$Description+"')")
        assert(ie.frame(:index, 1).button(:id, /CreateButton$/).exists?)
        ie.frame(:index, 1).button(:id, /CreateButton$/).click
      end
      def test_09_editAnnouncement
        sleep  5
        ie.div(:class,'CommonMessageActions').link(:text,'Edit').click
        assert(ie.frame(:index,1).text_field(:id,/MessageSubject$/).exists?)
        ie.frame(:index,1).text_field(:id,/MessageSubject$/).value = $SubjectEdit
        sleep 5
        ie.ie.Document.ParentWindow.execscript("Telligent_Modal._modalIframe.contentWindow.tinyMCE.get('ctl00_bcr_ctl01_ctl02_Message_ctl00_Editor').execCommand('mceSetContent', false,'"+$DescriptionEdit+"')")
        assert(ie.frame(:index, 1).button(:id, /CreateButton$/).exists?)
        ie.frame(:index, 1).button(:id, /CreateButton$/).click
        sleep 4
      end
      
      def test_10_deleteAnnouncement
        assert(ie.link(:id,/DeleteAnnouncement$/).exists?)
        sleep 5
        ie.link(:id,/DeleteAnnouncement$/).click
      end
      
    def test_99_Exit
        ie.close
    end

end 